package 代码记录.Bit48天集训;

import java.util.Scanner;

/**1.红与黑(搜索矩阵问题：类似于剑指offer-13)
 * 2.蘑菇阵
 * @author zx
 * @create 2022-06-27 20:18
 */
public class day_38 {
    /**
     * 改造成和剑指offer-13一样的做法
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            sc.nextLine();
            int x = 0, y = 0;
            boolean[][] used = new boolean[m][n];
            char[][] arr = new char[m][n];
            for (int i = 0; i < m; i++) {
                String str = sc.nextLine();
                for (int j = 0; j < n; j++) {
                    arr[i][j] = str.charAt(j);
                    if (arr[i][j] == '@') {//初始位置坐标
                        x = i;
                        y = j;
                    }
                }
            }
            int res = dfs(arr, x, y,used);
            System.out.println(res);
        }
    }
    public static int dfs(char[][] map, int m, int n,boolean[][] used) {
        if (m >= map.length || n >= map[0].length || m < 0 || n < 0 || map[m][n] == '#' || used[m][n]) {
            return 0;
        }
        used[m][n] = true;
        return 1 + dfs(map, m + 1, n,used) + dfs(map, m, n + 1,used)
                 + dfs(map, m, n - 1,used) + dfs(map, m - 1, n,used);
    }


    public static void main2(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            int k = sc.nextInt();
            //放置蘑菇,arr多开一行一列,因为题目的位置是从(1,1)开始的
            int[][] arr = new int[n + 1][m + 1];
            for(int i = 0;i < k;i++){
                int x = sc.nextInt();
                int y = sc.nextInt();
                arr[x][y] = 1;
            }
            System.out.printf("%.2f\n", sover(arr,n,m));
        }
    }
    private static double sover(int[][] map,int n,int m){
        double[][] dp = new double[n + 1][m + 1];
        dp[1][1] = 1.0;
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                //如果i或者j在边界,只能向一个方向移动,此时走到(i,j)位置的概率为1
                //当i或者j不在边界时,走到(i,j)的概率分别为0.5
                if(!(i == 1 && j == 1)){//除过起始位置
                    dp[i][j] = dp[i - 1][j] * (j == m ? 1.0 : 0.5) + dp[i][j - 1] * (i == n ? 1.0 : 0.5);
                }
                //(i,j)位置可能是蘑菇
                if(map[i][j] == 1){
                    dp[i][j] = 0.0;
                }
            }
        }
        return dp[n][m];
    }
}
